<?php
namespace app\common\controller;
use app\common\model\UserInfo;
use think\facade\Db;
use think\helper\Str;
use app\common\variable\MsgCode;
use app\common\model\Feedback as FeedbackModel;
use app\common\model\StatusAlias as StatusAliasModel;
use app\common\model\PushTask as PushTaskModel;
/**
 * 
 */
class Index extends Base
{
    public static function saveFeedback($D)
    {
        if (empty($D['type']))
            return parent::ajaxReturn('请选择反馈类型',MsgCode::$error);
        if (empty($D['contacts']))
            return parent::ajaxReturn('请输入联系方式',MsgCode::$error);
        if (empty($D['content']))
            return parent::ajaxReturn('请输入反馈内容',MsgCode::$error);
        $FeedbackModel=new FeedbackModel;
        $FeedbackModel->uid             =   $D['uid'];
        $FeedbackModel->type            =   $D['type'];
        $FeedbackModel->contacts        =   $D['contacts'];
        $FeedbackModel->content         =   $D['content'];
        $FeedbackModel->app_id          =   $D['app_id'];
        $FeedbackModel->alias_id        =   empty($D['alias_id'])?null:$D['alias_id'];
        $FeedbackModel->js_error        =   empty($D['js_error'])?'':json_encode($D['js_error']);
        $FeedbackModel->app_version     =   empty($D['app_version'])?'':$D['app_version'];
        $FeedbackModel->system_type     =   empty($D['system_type'])?'':$D['system_type'];
        $FeedbackModel->system_version  =   empty($D['system_version'])?'':$D['system_version'];
        $FeedbackModel->uuid            =   empty($D['uuid'])?'':$D['uuid'];
        $FeedbackModel->device_model    =   empty($D['device_model'])?'':$D['device_model'];
        $FeedbackModel->ui_mode         =   empty($D['ui_mode'])?'':$D['ui_mode'];
        $FeedbackModel->connection_type =   empty($D['connection_type'])?'':$D['connection_type'];
        $FeedbackModel->ip              =   request()->ip();
        $StatusAliasModel=StatusAliasModel::where(['alias'=>'feedback/type','value'=>$D['type']])->find();
        $userPhone=\app\common\model\User::where('id',$FeedbackModel->uid)->value('mobile');
        $AdminPushTaskModel=new PushTaskModel;
        $AdminPushTaskModel->event       =   'SystemWarning';
        switch ($D['type']) {
            case 6:
                $name=\app\common\model\Resume::where('id',$FeedbackModel->alias_id)->value('name');
                $PushData=[
                    'tpl'       =>'report_resume',
                    'level'     =>'举报简历通知',
                    'range'     =>'后台',
                    'time'      =>date('Y-m-d H:i:s'),
//                    'msg'       =>'举报简历ID:'.$FeedbackModel->alias_id
                    'msg'       =>"您有一条举报待处理，用户{$userPhone}举报了简历:{$name}(简历id:{$FeedbackModel->alias_id})，内容:{$FeedbackModel->content}"
                ];
                break;
            case 7:
                $name=\app\common\model\Jobs::where('id',$FeedbackModel->alias_id)->value('title');
                $PushData=[
                    'tpl'       =>'report_jobs',
                    'level'     =>'举报职位通知',
                    'range'     =>'后台',
                    'time'      =>date('Y-m-d H:i:s'),
//                    'msg'       =>'举报职位ID:'.$FeedbackModel->alias_id
                    'msg'       =>"您有一条举报待处理，用户{$userPhone}举报了职位:{$name}(职位id:{$FeedbackModel->alias_id})，内容:{$FeedbackModel->content}"
                ];
                break;
            default:
                $PushData=[
                    'tpl'       =>'feedback',
                    'level'     =>'用户反馈通知',
                    'range'     =>'后台',
                    'time'      =>date('Y-m-d H:i:s'),
//                    'msg'       =>'类型：'.$StatusAliasModel->title
                    'msg'       =>"您有一条反馈待处理，反馈类型:{$StatusAliasModel->title}，反馈内容:{$FeedbackModel->content}，联系方式:{$FeedbackModel->contacts}"
                ];
                break;
        }
        $AdminPushTaskModel->data        =   json_encode($PushData,JSON_UNESCAPED_UNICODE);
        // 启动事务
        Db::startTrans();
        try {
            $FeedbackModel->save();
            $AdminPushTaskModel->save();
            // 提交事务
            Db::commit();
            return parent::ajaxReturn('提交成功，请等待管理员处理');
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return parent::ajaxReturn('提交失败，请重试',MsgCode::$error);
        }
    }



    public static function EnterprisesToRemind($User){
        $AdminPushTaskModel=new PushTaskModel;
        $AdminPushTaskModel->event       =   'SystemWarning';
        $PushData=[
            'tpl'       =>'enterprises',
            'level'     =>'企业入驻提醒',
            'range'     =>'后台',
            'time'      =>date('Y-m-d H:i:s'),
//                'msg'       =>'企业用户：'.$User->mobile.'入驻了平台。'
            'msg'       => "您有新的企业入驻，注册手机号:{$User->mobile}，请及时跟进处理。"
        ];
        $AdminPushTaskModel->data        =  json_encode($PushData,JSON_UNESCAPED_UNICODE);
        return $AdminPushTaskModel;
    }
}